package com.xxxx.car.appointment.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.xxxx.car.appointment.vo.AppointmentVo;
import com.xxxx.car.common.core.domain.R;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xxxx.car.common.annotation.Log;
import com.xxxx.car.common.core.controller.BaseController;
import com.xxxx.car.common.core.domain.AjaxResult;
import com.xxxx.car.common.enums.BusinessType;
import com.xxxx.car.appointment.domain.Appointment;
import com.xxxx.car.appointment.service.IAppointmentService;
import com.xxxx.car.common.utils.poi.ExcelUtil;
import com.xxxx.car.common.core.page.TableDataInfo;

/**
 * 修养信息预约Controller
 * 
 * @author Huang
 * @date 2024-08-23
 */
@RestController
@RequestMapping("/appointment/info")
public class AppointmentController extends BaseController
{
    @Resource
    private IAppointmentService appointmentService;

    /**
     * 查询修养信息预约列表
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(Appointment appointment)
    {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return getDataTable(list);
    }

    /**
     * 导出修养信息预约列表
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:export')")
    @Log(title = "修养信息预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Appointment appointment)
    {
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        ExcelUtil<Appointment> util = new ExcelUtil<Appointment>(Appointment.class);
        util.exportExcel(response, list, "修养信息预约数据");
    }

    /**
     * 获取修养信息预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appointmentService.selectAppointmentById(id));
    }

    /**
     * 新增修养信息预约
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:add')")
    @Log(title = "修养信息预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AppointmentVo appointmentVo)
    {
        Date now = new Date();
        if(appointmentVo.getAppointmentTime().before(now)){
            throw new RuntimeException("预约到店时间不能早于当前时间");
        }
        Appointment appointment = new Appointment();
        BeanUtils.copyProperties(appointmentVo,appointment);
        return toAjax(appointmentService.insertAppointment(appointment));
    }

    /**
     *  客户到店
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:arrival')")
    @Log(title = "客户到店", businessType = BusinessType.UPDATE)
    @PutMapping("/arrival/{id}")
    public AjaxResult edit(@PathVariable Long id)
    {
        return toAjax(appointmentService.updateArrival(id));
    }

    /**
     * 修改修养信息预约
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:edit')")
    @Log(title = "修养信息预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppointmentVo appointmentVo)
    {
        return toAjax(appointmentService.updateAppointment(appointmentVo));
    }

    /**
     * 取消修养信息预约
     */
    @PreAuthorize("@ss.hasPermi('appointment:info:cancel')")
    @Log(title = "取消修养信息预约", businessType = BusinessType.UPDATE)
	@PutMapping("/cancel/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(appointmentService.cancelAppointment(id));
    }

    @Log(title = "修养信息预约", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/statements")
    public R createStatement(@PathVariable Long id){
        return R.ok(appointmentService.doCreateStatement(id));
    }
}
